package com.lagou.intercepter;

import com.lagou.model.RequestHeader;
import com.lagou.tool.HeaderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component

@Slf4j
public class AuthInfoInterceptor extends HandlerInterceptorAdapter {

    private static final String HTTP_TOKEN = "token";

//    @Override
//    public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
//        HttpHeaders headers = request.getHeaders();
//        headers.add("token","SESSIONID=b8dd5bd9-9fb7-48cb-a86b-e079cb554fb8");
//        log.info("拦截器已添加header");
//        return execution.execute(request,bytes);
//    }



    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        setRequestHeaderInfo(request);
        log.info("拦截成功11");
//        HttpServletRequest req=request;
//        response.addHeader("token",request.getSession().getAttribute("token")==null?"":request.getSession().getAttribute("token").toString());
////        headers.add("token","SESSIONID=b8dd5bd9-9fb7-48cb-a86b-e079cb554fb8");
        super.afterCompletion(request, response, handler, ex);
    }

//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        setRequestHeaderInfo(request);
//        log.info("拦截成功");
//        return true;
//    }

    private void setRequestHeaderInfo(HttpServletRequest request) {
        RequestHeader requestHeader = new RequestHeader();

        requestHeader.setTocken(request.getSession().getAttribute("token")==null?"":request.getSession().getAttribute("token").toString());

        //HeaderUtil是header在线程中的保存的工具类，方便线程中使用
        HeaderUtil.setRequestHeader(requestHeader);
    }

}
